/**
 * Created by chenzheyu on 2017/4/26.
 */
$(function () {
    var OnState = '';
    var limit = 20;
    var shopState={
        data:[
            {name:'全部',Val:''},
            {name:'已上架',Val:1},
            {name:'已下架',Val:0}
        ],
        onChange:function (e) {
            OnState=e.val;
        }
    };
$('.Top .SEO .shopState').DownPullSelect(shopState);
$('.Top .SEO .SEOBut').ActionButtons({data:'查询',onChange:function () {
    findVehicle(1, OnState)
}});


$('.AddButton').ActionButtons({data:' + 添加车系',onChange:function(){
	 window.location.href=ctx+'/rentvehicle/add';
}});
    function findVehicle(page, OnState) {
        $.ajax({
            url: ctx+'/rentvehicle/findVehicle',
            data: {
                'pager.offset': (page - 1) * limit,
                'limit': limit,
                isGrounding: OnState
            },
            type: 'GET',
            success: function (data) {
                $('.Mid .CustomPanelModule').remove();
                $('.Mid .PagingMdPagingMd').remove();
                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var vehicleId = data.content[index].id;
                        var findShopVehicleByVehicleId;
                        var OrderOption = {
                            data: {
                                Title: {
                                    content: {},
                                    onChange: function (e, action) {

                                        findShopVehicleByVehicleId=function(page) {
                                            var CustomPanelModuleContent = e.find('.CustomPanelModuleContent');
                                            var limit = 10;
                                            $.ajax({
                                                url: ctx+'/rentshop/findShopVehicleByVehicleId',
                                                data: {
                                                    vehicleId: data.content[index].id,
                                                    'limit': limit,
                                                    "pager.offset": (page - 1) * limit
                                                },
                                                type: 'GET',
                                                success: function (data) {
                                                    CustomPanelModuleContent.find('.PagingMdPagingMd').remove();
                                                    var findShopVehicleByVehicleIdOrder = {
                                                        data: {
                                                            Thead: {
                                                                shopName: '门店',
                                                                action: "操作"
                                                            },
                                                            Tbody: []
                                                        }
                                                    };
                                                    for (var i = 0; i < data.content.length; i++) {
                                                        (function () {
                                                            var index = i;
                                                            var vehicleId=data.content[index].vehicleId;
                                                            var val = {
                                                                shopName: data.content[index].shopName,
                                                                action: ''
                                                            };
                                                            var actions = $('<span class="action"></span>');
                                                            var sate=true;
                                                            actions.ActionButtons({
                                                                data: '关闭', onChange: function () {
                                                                	
                                                                    if(sate){
                                                                    	
                                                                    	sate=false;
                                                                    	 var dredgeShopVehicle={
                                                                                 data:{
                                                                                     Title:"删除提示",
                                                                                     Content:'是否要删除该门店？',
                                                                                     OkChange:function () {
                                                                                         $.ajax({
                                                                                             url:ctx+'/rentshop/closeShopVehicle',
                                                                                             data:{
                                                                                                 id: data.content[index].id
                                                                                             },
                                                                                             type:'POST',
                                                                                             success:function (data) {
                                                                                            	 sate=true
                                                                                                 if(data.message=='关闭车系成功'){
                                                                                                     $('body').Overlays({data:{text:'关闭成功！',state:'Ok'}});
                                                                                                     findShopVehicleByVehicleId(1, OnState);
                                                                                                 }else {
                                                                                                     $('body').Overlays({data:{text:'关闭失败，请稍后重试！',state:'No'}});
                                                                                                     findShopVehicleByVehicleId(1, OnState);
                                                                                                 }
                                                                                             }
                                                                                         })
                                                                                     },
                                                                                     CancelChange:function(){
                                                                                    	 sate=true
                                                                                     }
                                                                                     
                                                                                 }
                                                                             };
                                                                             $('body').ModalBox(dredgeShopVehicle)
                                                                    }
                                                                   
                                                                }
                                                            });
                                                            val.action = actions;

                                                            findShopVehicleByVehicleIdOrder.data.Tbody.push(val)
                                                        })()
                                                    }
                                                    CustomPanelModuleContent.Order(findShopVehicleByVehicleIdOrder);
                                                    var PagingMdOption = {
                                                        data: {
                                                            rows: limit,
                                                            nowrows: page,
                                                            Pages: '',
                                                            total: data.totalElements,
                                                            change: function (index) {
                                                                findShopVehicleByVehicleId(index);
                                                            }
                                                        }
                                                    };
                                                    CustomPanelModuleContent.PagingMd(PagingMdOption);
                                                    action.successUpLoad();
                                                }
                                            });

                                        };

                                        findShopVehicleByVehicleId(1)
                                    }
                                },
                                Tbody: ""
                            }
                        };
                        var TitleObj = {
                            name: data.content[index].name,
                            rent: data.content[index].rent,
                            illegalDeposit: data.content[index].illegalDeposit,
                            brandName: data.content[index].brandName,
                            transmission: data.content[index].transmission,
                            type: data.content[index].type,
                            displacement: data.content[index].displacement,
                            seat: data.content[index].seat,
                            action: data.content[index].action
                        };

                        var action = $('<div class="action"></div>');
                        action.ActionButtons({
                            data: '编辑', onChange: function () {
                                window.location.href=ctx+'/rentvehicle/update?id='+vehicleId;
                            }
                        });

                        switch (data.content[index].isGrounding) {
                            case 0:
                                action.ActionButtons({
                                    data: '上架', onChange: function () {
                                        $.ajax({
                                            url: ctx+'/rentvehicle/grouding',
                                            data: {
                                                id: data.content[index].id,
                                                isGrounding: 1
                                            },
                                            type: 'POST',
                                            success: function (data) {
                                                if (data.message == '车系上架成功') {
                                                    $('body').Overlays({data: {text: '上架成功！', state: 'Ok'}});
                                                    findVehicle(1, OnState)
                                                } else {
                                                    $('body').Overlays({data: {text: '上架失败，请稍后重试！', state: 'No'}})
                                                }
                                            }
                                        })
                                    }
                                });
                                break;
                            case 1:
                                action.ActionButtons({
                                    data: '下架', onChange: function () {
                                        $.ajax({
                                            url: ctx+'/rentvehicle/grouding',
                                            data: {
                                                id: data.content[index].id,
                                                isGrounding: 0
                                            },
                                            type: 'POST',
                                            success: function (data) {
                                                if (data.message == '车系下架成功') {
                                                    $('body').Overlays({data: {text: '下架成功！', state: 'Ok'}});
                                                    findVehicle(1, OnState);
                                                    window.reload();
                                                } else {
                                                    $('body').Overlays({data: {text: '下架失败，请稍后重试！', state: 'No'}})
                                                }
                                            }
                                        })
                                    }
                                });
                                break;
                        }

                        action.ActionButtons({
                            data: '开通车型', onChange: function () {
                                var ModuleOption = {
                                    data: {
                                        Title: '开通车型',
                                        Content: ''
                                    }
                                };
                                var limit = 10;
                                var SEOval = '';
                                var ContentModule = $('<div class="ContentModule"></div>');
                                ModuleOption.data.Content = ContentModule;
                                function selectRentshopByShopName(page, SEOval) {
                                    $.ajax({
                                        url: ctx+'/rentshop/selectRentshops',
                                        data: {
                                            companyId: '',
                                            shopName: SEOval,
                                            vehicleId: vehicleId,
                                            'limit': limit,	
                                            "pager.offset": (page - 1) * limit
                                        },
                                        type: 'GET',
                                        success: function (data) {
                                            $('body .CustomModalBox[custommodalboxlength="0"] .ContentModule  ').html('');
                                            var ShopNameOrderMod = $("<div class='ShopNameOrder'></div>");
                                            var SEOBut = $('<span class="SEOBut"></span>');
                                            SEOBut.ActionButtons({
                                                data: '查询', onChange: function () {
                                                    SEOval = ContentModule.find('input').val();
                                                    selectRentshopByShopName(1, SEOval);
                                                }
                                            });
                                            ContentModule.append('<div class="SEO CustomClear"><input type="text" placeholder="请输入门店名称"></div>');
                                            ContentModule.find('.SEO').append(SEOBut);
                                            var ShopNameOrder = {
                                                data: {
                                                    Thead: {
                                                        shopName: '门店',
                                                        companyName: '分公司',
                                                        address: '地址',
                                                        contacts: '联系人',
                                                        telephone: '手机号',
                                                        phone: '座机号',
                                                        action: '操作'
                                                    },
                                                    Tbody: []
                                                }
                                            };
                                            for (var i = 0; i < data.content.length; i++) {
                                                (function () {
                                                    var index = i;
                                                    var val = {
                                                        shopName: data.content[index].shopName,
                                                        companyName: data.content[index].companyName,
                                                        address: data.content[index].address,
                                                        contacts: data.content[index].contacts,
                                                        phone: data.content[index].phone,
                                                        telephone: data.content[index].telephone,
                                                        action: ''
                                                    };
                                                    var action = $('<div class="action"></div>');
                                                    action.ActionButtons({
                                                        data: '开通', onChange: function () {
                                                            var dredgeOption = {
                                                                data: {
                                                                    Title: "开通提示",
                                                                    Content: '是否要为该公司开通此车型！',
                                                                    OkChange: function (e, Remove) {
                                                                        $.ajax({
                                                                            url: ctx+'/rentshop/dredgeShopVehicle',
                                                                            data: {
                                                                                shopId: data.content[index].id,
                                                                                vehicleId: vehicleId
                                                                            },
                                                                            type: 'POST',
                                                                            success: function (data) {

                                                                                if (data.message == '开通车系成功') {
                                                                                    $('body').Overlays({
                                                                                        data: {
                                                                                            text: '开通成功！',
                                                                                            state: "Ok"
                                                                                        }
                                                                                    });
                                                                                    Remove();
                                                                                    $('.CustomModalBox').remove();
                                                                                    findShopVehicleByVehicleId(1)
                                                                                } else {
                                                                                    Remove();
                                                                                    $('body').Overlays({
                                                                                        data: {
                                                                                            text: '开通失败功，请稍后重试！',
                                                                                            state: "No"
                                                                                        }
                                                                                    })
                                                                                }
                                                                            }
                                                                        })
                                                                    }
                                                                },
                                                                OKHide: true
                                                            };

                                                            $('body').ModalBox(dredgeOption)
                                                        }
                                                    });
                                                    val.action = action;
                                                    ShopNameOrder.data.Tbody.push(val)
                                                })()
                                            }
                                            ShopNameOrderMod.Order(ShopNameOrder);
                                            ContentModule.append(ShopNameOrderMod);
                                            var PagingMdOptions = {
                                                data: {
                                                    rows: limit,
                                                    nowrows: page,
                                                    Pages: '',
                                                    total: data.totalElements,
                                                    change: function (index) {
                                                        selectRentshopByShopName(index, SEOval);
                                                    }
                                                }
                                            };
                                            var PagingMds = $('<div class="PagingMds"></div>');
                                            PagingMds.PagingMd(PagingMdOptions);
                                            ContentModule.append(PagingMds)
                                        }
                                    });

                                }

                                selectRentshopByShopName(1, SEOval);

                                $('body').ModalBox(ModuleOption)
                            }
                        });

                        TitleObj.action = action;
                        var rows = $('<div class="rows"></div>');
                        for (key in TitleObj) {
                            var list = $('<div class="list"></div>').append(TitleObj[key]);
                            rows.append(list)
                        }
                        OrderOption.data.Title.content = rows;


                        $('.Mid').PanelModule(OrderOption);


                    })()

                }
                var PagingMdOption = {
                    data: {
                        rows: limit,
                        nowrows: page,
                        Pages: '',
                        total: data.totalElements,
                        change: function (index) {
                            findVehicle(index, OnState);
                        }
                    }
                };
                $('.Mid').PagingMd(PagingMdOption);
            }
        });
    }

    findVehicle(1, OnState)


});